import React from 'react';
import {inject, observer} from 'mobx-react';
import {toJS} from 'mobx';
import { WeaDialog,WeaNewScroll,WeaBrowser,WeaUpload,WeaAuth,WeaTools, WeaTab, WeaButtonIcon,WeaRightMenu} from 'ecCom';
import {Button,Spin,Tabs,Row,Col,Input,Checkbox, Icon ,Modal, Tooltip} from 'antd';
import {WeaTableNew} from 'comsMobx';
import {WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
const WeaTable = WeaTableNew.WeaTable;

@inject('myWorkPlanStore')
@observer
class Share extends React.Component {
  constructor(props) {
	  super(props);
  }
  componentDidMount() {
    const {doGetShareTable} = this.props.myWorkPlanStore
    doGetShareTable(this.props.workPlanId);
    this.props.spinning(false);
 }

  componentWillReceiveProps(nextProps) {
  }

  componentWillUnmount() {
    //组件卸载时一般清理一些状态
    let _this = this;
    _this.props.changeOneWeekDatas && _this.props.changeOneWeekDatas();
  }
  getRightMenu = () => {
    let btnArr = [];
    let _this = this;
		let {comsWeaTableStore} = this.props.myWorkPlanStore;
		comsWeaTableStore = toJS(comsWeaTableStore);
    const tableNow = comsWeaTableStore;
    btnArr.push({
      key: 'Add',
      content: getLabel(384113, '添加'),
      icon: <i className='icon-coms-Add-to-o' />,
      onClick: this.addShare,
    })
    if(tableNow && tableNow.selectedRowKeys.length==0){
      btnArr.push({
        key: 'Delete',
        disabled: true,
        content: getLabel(91, '删除'),
        icon: <i className='icon-coms-delete' />,
        onClick: this.deledtShare,
      })
    }else{
      btnArr.push({
        key: 'Delete',
        content: getLabel(91, '删除'),
        icon: <i className='icon-coms-delete' />,
        onClick: this.deledtShare,
      })
    }
    btnArr.push({
      key: 'doReturn',
      content: getLabel(1290,'返回'),
      icon: <i className='icon-coms-Reset' />,
      onClick: this.props.rightButton,
    })
    return btnArr;
  }
  render() {
    let {dataKey,comsWeaTableStore} = this.props.myWorkPlanStore;
    const tableNow = comsWeaTableStore
    let {conditions,visible} = this.props;
    return (
      <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@tl5r68`} 
        datas={this.getRightMenu()}
        collectParams={{
          favname:'共享日程',
          favouritetype: 2,
          objid: 0,
          importantlevel:1,
        }}
        >
      <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@rnkjuw`} height={this.props.isSingle ?  '100%': '500px'}>
			<div className="workPlan-share" id="workPlan-share-id">
        <WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@cv1ehx`}
          datas={[
            {
              title: `${getLabel(1279,'共享信息')}`,
              viewcondition: 0
            }
          ]}
          keyParam="viewcondition"  //主键
          countParam="groupid" //数量
          buttons={[
            <WeaButtonIcon ecId={`${this && this.props && this.props.ecId || ''}_WeaButtonIcon@wuojnh@add`} buttonType='add' type="primary" disabled={false } size="small" onClick={this.addShare}/>,
            <WeaButtonIcon ecId={`${this && this.props && this.props.ecId || ''}_WeaButtonIcon@8pkr9x@del`} buttonType='del' type="primary" disabled={tableNow && tableNow.selectedRowKeys.length>0?false:true } onClick={this.deledtShare}/>,
          ]}
        />
        <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@3wt6sf`} 
          sessionkey={dataKey}
          comsWeaTableStore={comsWeaTableStore}
          hasOrder={true}
          needScroll={false}
          onOperatesClick={this.onOperatesClick}
         />
        <WeaAuth ecId={`${this && this.props && this.props.ecId || ''}_WeaAuth@8oep2s`}
          visible={visible}
          conditions={conditions}
          icon={'icon-coms-schedule'}
          iconBgcolor={'#6D3CF8'}
          onOk={v => this.onOk(v)}
          onCancel={() => this.onCancel()}
        /> 
			</div>
      </WeaNewScroll >
      </WeaRightMenu>
    )
  }
  onOperatesClick = (record,index,operate,flag)=>{
    let {workPlanId} = this.props
    record = toJS(record);
    let {id} = record;
    operate = toJS(operate);
    let values = {delid:id,planID:workPlanId}

    const {doGetShareTable} = this.props.myWorkPlanStore
    Modal.confirm({
      title: `${getLabel(131329,'信息确认')}`,
      content: `${getLabel(15097,'确定要删除吗?')}`,
      onOk() {
        WeaTools.callApi('/api/workplan/share/deleteWorkPlanShare', 'POST', values).then((result)=>{
          result.status && doGetShareTable(workPlanId);
        })
      },
      onCancel() {},
    });
  }
  onOk = (v)=>{
    let values = {...v, planID:this.props.workPlanId};
    const {doGetShareTable} = this.props.myWorkPlanStore;
    WeaTools.callApi('/api/workplan/share/addWorkPlanShare', 'POST', values).then((result)=>{
      result.status && this.onCancel();
      result.status && doGetShareTable(this.props.workPlanId);
    })
  }

  onCancel = ()=>{
    this.props.onCancel && this.props.onCancel();
  }
  deledtShare = ()=>{
		//共享里的删除
		let _this = this;
		let {dataKey,comsWeaTableStore,doGetShareTable} = this.props.myWorkPlanStore;
		let {workPlanId} =  this.props;
		comsWeaTableStore = toJS(comsWeaTableStore);
    const tableNow = comsWeaTableStore
		if(tableNow && tableNow.selectedRowKeys.length==0){
			Modal.warning({
			title: `${getLabel(127354,'警告')}`,
			content: `${getLabel(32568,'请选择要删除的记录!')}`,
			});
		}else if(tableNow && tableNow.selectedRowKeys.length>0){
			let keys = tableNow.selectedRowKeys.join(',');
			let values = {delid:keys,planID:workPlanId};
			Modal.confirm({
				title: `${getLabel(131329,'信息确认')}`,
        content: `${getLabel(15097,'确定要删除吗?')}`,
				onOk() {
          // _this.setState({spinning:true})
          let obj = {spinning:true}
          _this.props.callbackState(obj)
					WeaTools.callApi('/api/workplan/share/deleteWorkPlanShare', 'POST', values).then((result)=>{
						if(result.status){
							doGetShareTable(workPlanId);
              let objF = {spinning:false}
              _this.props.callbackState(objF)
						} 
					})
				},
				onCancel() {},
			});
		}
	}
	addShare = ()=>{
		//共享里的添加
		WeaTools.callApi('/api/workplan/share/getShareAuthField', 'POST', {workplanId:this.props.workPlanId}).then((result)=>{
      this.setState({authConditions:result,authVisible:true})
      let obj = {authConditions:result,authVisible:true}
      this.props.callbackState(obj)
		})
	}
 
	

}

export default Share
